package com.niit.qcx.companyPrice;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**Reducer阶段：
 对相同中介公司的价格进行汇总再计算平均价格
 输出中介公司名称和平均价格，如<正顺地产, 7553元/㎡>
 */

public class compPReducer extends Reducer<Text, FloatWritable, Text,String> {
    @Override
    protected void reduce(Text key, Iterable<FloatWritable> values, Context context) throws IOException, InterruptedException {
        List<Float> prices = new ArrayList<>();
        float sum = 0;
        float count = 0;

        for (FloatWritable val : values) {
            sum += val.get();//循环累加同公司单价
            count++;
        }

        String ave_uni_Pri = String.valueOf(sum /count);
        context.write(key, "  " + ave_uni_Pri + "元/㎡");
    }
}
